package com.bdtop.dbl.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import com.bdtop.dbl.R;

public class RadarView extends View {
    
    private Bitmap mDefaultDeviceBitmap;
    private Bitmap mCenterPointBitmap;
    private Bitmap mRadarBitmap;
    private Bitmap mScanningBitmap;
    private boolean isScanning = false;
    
    private int mCenterX;
    private int mCenterY;
    
    private Handler mHandler;
    private float mAngleOffset = 0;
    
    private Bitmap mDeviceBitmap;
    private float mDeviceRange;
	
	public RadarView(Context context) {
		super(context);
		setDefaultBitmap();
	}
	
	public RadarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setDefaultBitmap();
	}

	public RadarView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setDefaultBitmap();
	}
	
    private void setDefaultBitmap() {
        mDefaultDeviceBitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.radar_device_location);
        setCenterPointDrawable(R.drawable.radar_center);
	    setRadarDrawable(R.drawable.radar_bg);
	    setScanningDrawable(R.drawable.radar_scanning);
	}
	
	/**
	  * TODO
	  * @param 
	  * @return void
	  * @throws
	 */
	public void startScanning() {
	    isScanning = true;
	    mHandler = new Handler();
	    mHandler.post(mRefreshRunnable);
	}

	/**
	  * TODO
	  * @param 
	  * @return void
	  * @throws
	 */
	public void stopScanning() {
	    isScanning = false;
	    if (mHandler != null) {
	        mHandler.removeCallbacks(mRefreshRunnable);
	        mHandler = null;
        }
	}
	
	// The runnable to refresh the scanning view.
	private Runnable mRefreshRunnable = new Runnable() {
        
        @Override
        public void run() {
            mAngleOffset += 3;
            invalidate();
            mHandler.postDelayed(this, 30);
        }
    };
	
	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		mCenterX = getWidth() / 2;
		mCenterY = getHeight() / 2;
		
        // draw background
        canvas.drawBitmap(mRadarBitmap, mCenterX - mRadarBitmap.getWidth() / 2, mCenterY
                - mRadarBitmap.getHeight() / 2, null);
        
        // draw center point
        canvas.drawBitmap(mCenterPointBitmap, mCenterX - mCenterPointBitmap.getWidth() / 2,
                mCenterY - mCenterPointBitmap.getHeight() / 2, null);

        if (mDeviceBitmap != null) {
            float x = mCenterX + mDeviceRange * 10;
            float y = mCenterY;
            canvas.drawBitmap(mDeviceBitmap, x - mDeviceBitmap.getWidth() / 2,
                    y - mDeviceBitmap.getWidth() / 2, null);
        }

        if (!isScanning) {
            return;
        }
		
        if (mAngleOffset == 0) {
            canvas.drawBitmap(mScanningBitmap, mCenterX - mScanningBitmap.getWidth(), mCenterY,
                    null);
        }
        else {
            Rect rMoon = new Rect(mCenterX - mScanningBitmap.getWidth(), mCenterY, mCenterX,
                    mCenterY + mScanningBitmap.getHeight());

            canvas.rotate(mAngleOffset, mCenterX, mCenterY);
            canvas.drawBitmap(mScanningBitmap, null, rMoon, null);
        }
	}
	
    public void setDeviceLocation(Bitmap device, float distance) {
        mDeviceBitmap = device;
        mDeviceRange = distance;
        invalidate();
    }
    
    public void setDeviceLocation(float distance) {
        setDeviceLocation(mDefaultDeviceBitmap, distance);
    }

	/**
     * @return mCenterPointBitmap
     */
    public Bitmap getCenterPointBitmap() {
        return mCenterPointBitmap;
    }

    /**
     * @param mCenterPointBitmap the mCenterPointBitmap to set
     */
    public void setCenterPointBitmap(Bitmap centerPointBitmap) {
        this.mCenterPointBitmap = centerPointBitmap;
    }
    
    public void setCenterPointDrawable(int resId) {
        this.mCenterPointBitmap = BitmapFactory.decodeResource(getResources(), resId);
    }

    /**
     * @return mRadarBitmap
     */
    public Bitmap getRadarBitmap() {
        return mRadarBitmap;
    }

    /**
     * @param mRadarBitmap the mRadarBitmap to set
     */
    public void setRadarBitmap(Bitmap radarBitmap) {
        this.mRadarBitmap = radarBitmap;
    }
    
    public void setRadarDrawable(int resId) {
        this.mRadarBitmap = BitmapFactory.decodeResource(getResources(), resId);
    }

    /**
     * @return mScanningBitmap
     */
    public Bitmap getScanningBitmap() {
        return mScanningBitmap;
    }

    /**
     * @param mScanningBitmap the mScanningBitmap to set
     */
    public void setScanningBitmap(Bitmap scanningBitmap) {
        this.mScanningBitmap = scanningBitmap;
    }
    
    public void setScanningDrawable(int resId) {
        this.mScanningBitmap = BitmapFactory.decodeResource(getResources(), resId);
    }
}